﻿/**
 * VOCUS Implementation by Fan Zhang
 * Psychology Department, Peking University
 * 2013.1.12
 * Reference:
 * Frintrop, S. (2006). VOCUS: A visual attention system for object detection and goal-directed search. 
 */

//还要实现watershed，和其他几种normalize的算法，以及加入top-down
//	off-center 效果不好
//	gabor的参数，实部，虚部
//	cross-scale加法具体怎么做
//  local maximum 怎么算
//	种子是4连通还是8连通
//	adaptive
//  color de on center太暗了
//	除以m就太暗了

//Feature			learning Weight
//intensity on/off	0.001
//intensity off/on	9.616
//orientation 0◦		4.839 
//orientation 45◦	9.226
//orientation 90◦	2.986
//orientation 135◦	8.374 
//color green		76.572
//color blue		4.709
//color red			0.009 
//color yellow		0.040 
//conspicuity I		6.038
//conspicuity O		5.350
//conspicuity C		12.312
//from "Goal-Directed Search with a Top-Down Modulated System"

//#include <opencv2\core\core.hpp>
//#include <opencv2\imgproc\imgproc.hpp>
//#include <opencv2\highgui\highgui.hpp>
//#include <opencv2\features2d\features2d.hpp>
#include <opencv.hpp>
#include <iostream>
#include <string>
#include <time.h>

#include "cvgabor.h"

using namespace std;
using namespace cv;

enum ParameterName{
	VAM_NAVIE_SUM = 0,
	VAM_LINEAR_COMBINATIOIN = 1,
	VAM_NON_LINEAR_AMPLIFICATION = 2,
	VAM_ITERATIVE = 3
};

class VOCUS
{
public:
	VOCUS(void);
	~VOCUS(void);

	bool loadImage(IplImage* img);
	bool loadImageFromPath(string );
	bool loadConfig(string );
	bool loadConfig(FileStorage fs);
	bool writeConfigFile(FileStorage fs);
	//	Revise parameter, tranfer pairs of parameter name and value
	bool setParameters(vector<ParameterName>, vector<float>);

	void run();
	//	Get Image
	IplImage* getOrigin();
	IplImage* getResult();
	IplImage* getSaliencyMap();

private:

	//	Images :
	//	Input Image
	IplImage* _originImg;
	string _imgPath;
	//	Image Pyramids
	vector<IplImage*> _IntImgPyr;
	vector<IplImage*> _OriLapPyr;	//	A pyramid for each direction 
	vector<vector<IplImage*>> _ColImgPyr;	//	Color * Level A pyramid for each color, red, green, blue, yellow
	//	Scale Maps
	vector<vector<vector<IplImage*>>> _IntScaleImgPyr;	//	On-center & Off-center (2)  * Number of Surround Size (2) * Number of Pyramid layer (3)
	vector<vector<IplImage*>> _OriScaleImgPyr;	//	Directions (4) * Number of Pyramid Layer (3)
	vector<vector<vector<IplImage*>>> _ColScaleImgPyr;	//	Number of Colors (4) * Number of Surround Size (2) * Number of Pyramid layer (3) 
	//	Feature Maps
	vector<IplImage*> _IntFeatMaps;	// On-center & Off-center (2)
	vector<IplImage*> _OriFeatMaps;	// Direction (4)
	vector<IplImage*> _ColFeatMaps;	// Color (4)
	//	Conspicuity Map
	IplImage* _IntConspImg;
	IplImage* _OriConspImg;
	IplImage* _ColConspImg;
	//	Saliency Map;
	IplImage* _SaliencyMap;
	//	Colored Result
	IplImage* _ColorResult;
	//	Parameters :
	//	Images Array Size
	int _numOfPyrLevel;
	int _numOfScalePyrLevel;
	int _numOfPyrDiff;
	int _numOfOri;
	int _numOfColor;
	int _numOfSurScale;
	int _OnOffsize;

	//	
	vector<int> _surr_scale;
	vector<float> _ori_angle;
	//Parameter of Gabor

	double _gabor_sigma;
	double _gabor_f;
	int _gabor_scale;

	double _localMaxThres_ratio2Global;
	double _floodfillRatio;

	//	Output Results :
	bool _showProcess;
	//	Most Salieny Region

	//	Focus of View

	//	

private:
	//	Compute image pyramids
	void _computeIntPyr();
	void _computeOriLapPyr3x3();
	void _computeOriLapPyr5x5();
	void _computeColPyr();
	//	Compute images of different scales using center-surround algorithm
	void _computeScaleIntImgs();
	void _OnCenterFilter(IplImage* src, IplImage* dst, int);
	void _OffCenterFilter(IplImage* src, IplImage* dst ,int);
	void _computeScaleOriImgs();
	void _makeGaborFilter();
	void _computeScaleColImgs();
	//	Compute feature maps using across-scale addition
	void _computeIntFeatureMaps();
	void _computeOriFeatureMaps();
	void _computeColFeatureMaps();
	//	Compute conspicuity maps
	void _computeIntConsMap();
	void _computeOriConsMap();
	void _computeColConsMap();
	void _findLocalMaximum(IplImage*, int* numLocalMax, double* Mmax);
	//	Compute saliency map
	void _computeSaliencyMap();
	//	Compute most salient regions
	void _computeMSR();
	//	Utility
	void _init();
	void _release();
	float _dist(Point2i, Point2i);
	IplImage* _Gaussian3x3Smooth(IplImage* );
<<<<<<< .mine
	void _showPyr(string winname, vector<IplImage*>);
	void _showSingleImage(string winname, IplImage*);
	void _show32FImage(char*, IplImage* );
	void _ab2rgyb(int a, int b, int* red, int* green, int* yellow, int* blue);
=======
	void _showPyr(vector<IplImage* >);
	void _show32FImage(char*, IplImage* );
	void _ab2rgyb(int a, int b, int* red, int* green, int* yellow, int* blue);
>>>>>>> .r5
};

